package com.wing.cms.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.wing.cms.domain.Layout;
import com.wing.common.CommonSpringJDBC;
import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Auth;

@Repository
public class LayoutDao extends CommonSpringJDBC<Layout, String> {

	public PageBean findPageList(int currentPage, int pageSize, String siteId) {
		String where= " 1=1 ";
		String sql1=" SELECT * FROM cms_layout where "+where+" ORDER BY id ASC " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(id) from  cms_layout  where"+where;
		
		if(!Constants.WORK_SITE.equals(siteId)){
			where = " t.ispub='"+Layout.ISPUB_TRUE+"' or (a.type='"+Auth.TYPE_LAYOUT+"' and a.owner_id='"+siteId+"' and a.res_id=t.id) ";
			sql1=" SELECT distinct t.* FROM cms_layout t,sys_auth a where "+where+" ORDER BY t.order_num ASC " +
				" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
			sql2 = "select count(t.id) FROM cms_layout t,sys_auth a  where"+where;
		}
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public List<Map<String, Object>> findAll(String siteId) {
		String where= " 1=1 ";
		String sql1=" SELECT * FROM cms_layout where "+where+" ORDER BY id ASC " ;	
		if(!Constants.WORK_SITE.equals(siteId)){
			where = " t.ispub='"+Layout.ISPUB_TRUE+"' or (a.type='"+Auth.TYPE_LAYOUT+"' and a.owner_id='"+siteId+"' and a.res_id=t.id) ";
			sql1=" SELECT distinct t.* FROM cms_layout t,sys_auth a where "+where+" ORDER BY id ASC " ;
		}
		return super.getSimpleJdbcTemplate().queryForList(sql1);
	}
	
}
